package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.CommodityVo;
import com.ruoyi.system.service.ICollectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Commodity;
import com.ruoyi.system.service.ICommodityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2024-03-10
 */
@RestController
@RequestMapping("/system/commodity")
public class CommodityController extends BaseController
{
    @Autowired
    private ICommodityService commodityService;
    /**
     * 查询商品列表
     */
    //@PreAuthorize("@ss.hasPermi('system:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Commodity commodity)
    {
        startPage();
        List<CommodityVo> list = commodityService.selectCommodityList(commodity);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    //@PreAuthorize("@ss.hasPermi('system:commodity:export')")
//    @Log(title = "商品", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Commodity commodity)
//    {
//        List<CommodityVo> list = commodityService.selectCommodityList(commodity);
//        ExcelUtil<Commodity> util = new ExcelUtil<Commodity>(Commodity.class);
//        util.exportExcel(response, list, "商品数据");
//    }

    /**
     * 获取商品详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:commodity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commodityService.selectCommodityById(id));
    }

    /**
     * 新增商品
     */
    //@PreAuthorize("@ss.hasPermi('system:commodity:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody Commodity commodity)
    {
        commodity.setSellerId(getUserId());
        return toAjax(commodityService.insertCommodity(commodity));
    }

    /**
     * 修改商品
     */
    //@PreAuthorize("@ss.hasPermi('system:commodity:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commodity commodity)
    {
        if (commodityService.selectCommodityById(commodity.getId())==null) {
            return error("当前id没有操作权限！");
        }
        commodity.setSellerId(getUserId());
        return toAjax(commodityService.updateCommodity(commodity));
    }

    /**
     * 删除商品
     */
    //@PreAuthorize("@ss.hasPermi('system:commodity:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(Long id:ids){
            CommodityVo commodity = commodityService.selectCommodityById(id);
            if (commodity==null) {
                if (!getUserId().equals(commodity.getSellerId())){
                    return error("包含非法的id值！");
                }
            }
        }
        return toAjax(commodityService.deleteCommodityByIds(ids));
    }
}
